package com.synchrona.updateSyncManager;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.ParseConversionEvent;
import org.apache.woden.tool.converter.Convert;

import com.synchrona.account.Account;
import com.synchrona.common.Common;
import com.synchrona.common.EnumService;
import com.synchrona.common.EnumUpdateStatus;
import com.synchrona.dataProcessor.DataProcessor;
import com.synchrona.syncDataAccess.*;
import com.synchrona.update.Update;
import com.synchrona.user.User;
import com.synchrona.pluginManager.*;

public class UpdateSyncManager {

	public UpdateSyncManager() {

	}

	//Main method called by other components for update synchronization
	public void synchronizeUpdate(Update update) {
		SyncDataAccess access = new SyncDataAccess();
		try {
			DataProcessor processor = new DataProcessor(update);
			update = processor.process();
			String name = null;
			String currentStat = update.getData();
			synchronize(update);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	// Synchronizes the update
	public void synchronize(Update update) {

		User user = (User) update.getUser();
		int eliminateAccountIndex = 0;
		String userName = user.getUserName();
		String blogUrl = null;
		com.synchrona.pluginManager.PluginManager manager = new com.synchrona.pluginManager.PluginManager();
		try {
			System.out.println("Getting the user........");
			user = getUser(userName);
			List<Account> accountList = (List<Account>) user.getAccountList();
			if (user != null) {

				int currentServiceId = (int) update.getService().index();
				int userIntoTen = (int) (user.getUserId()) * 10;
				int currentAccountId = (userIntoTen + currentServiceId);
				System.out.println("Processing user accounts..........");
				if (!accountList.isEmpty()) {
					for (Account account : accountList) {
						String id = Integer.toString(currentAccountId);
						if (account.getAccountId().equals(id)) {
							if (update.getService() == EnumService.Blogger) {
								String blogTitle = update.getData();
								Blogger blogger = new Blogger();
								DataProcessor processor = new DataProcessor();
								blogUrl = blogger.getBlogURL(update.getData(),
										account);
								String tinyURL = processor.getTinyUrl(blogUrl);
								update.setData("Blog updated: " + blogTitle
										+ ":" + tinyURL);

							}
							eliminateAccountIndex = accountList
									.indexOf(account);
							if (!accountList.isEmpty())
								continue;
							else
								break;
						}
					}
				}
				accountList.remove(eliminateAccountIndex);
				System.out.println("Number of Acoounts :" + accountList.size());
				user.setAccountList(accountList);
				update.setUser(user);
				manager.synchronizeStatus(update);
				update.setUpdateStatus(EnumUpdateStatus.Success);
				System.out.println(update.getUpdateStatus().toString());
				updateDatabase(update);
			}
		} catch (Exception e) {
			update.setUpdateStatus(EnumUpdateStatus.Pending);
			updateDatabase(update);
			e.printStackTrace();
		}

	}

	// /Returns the user on the user name
	public User getUser(String userName) {
		User user = new User();
		SyncDataAccess access = new SyncDataAccess();
		try {
			user = access.getUser(userName);
			if (user != null) {
				List<Account> accountList = new ArrayList<Account>();
				accountList = getUserAccounts((int) user.getUserId());
				user.setAccountList(accountList);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}

	// Returns the user account lists
	public List<Account> getUserAccounts(int userId) {
		List<Account> userAccounts = new ArrayList<Account>();
		try {
			SyncDataAccess access = new SyncDataAccess();
			userAccounts = access.getUserAccounts(userId);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return userAccounts;
	}
	
	public void updateDatabase(Update update){
		try {
			SyncDataAccess access = new SyncDataAccess();
			access.updateDatabase(update);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
